/*
 *
 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun 
 * Microsystems, Inc.  Use is subject to license terms.
 * 
 */
/**
 * The SimpleTopicPublisher class consists only of a main method,
 * which publishes several messages to a topic.
 * 
 * Run this program in conjunction with SimpleTopicSubscriber.  
 * Specify a topic name on the command line when you run the
 * program.  By default, the program sends one message.  
 * Specify a number after the topic name to send that number 
 * of messages.
 */
package sbc.jms;

import java.util.Properties;
import javax.jms.*;
import javax.naming.*;
import sbc.fabrik.model.Platine;

public class SimpleTopicPublisher {


    /**
     * Main method.
     *
     * @param args     the topic used by the example and,
     *                 optionally, the number of messages to send
     */
    public static void main(String[] args) {
        String                  topicName = null;
        Context                 jndiContext = null;
        TopicConnectionFactory  topicConnectionFactory = null;
        TopicConnection         topicConnection = null;
        TopicSession            topicSession = null;
        Topic                   topic = null;
        javax.jms.TopicPublisher          topicPublisher = null;
        ObjectMessage             message = null;
        final int               NUM_MSGS;
        
        topicName = "topic/sbcTopic";
        System.out.println("Topic name is " + topicName);
            NUM_MSGS = 10;
        
        /* 
         * Create a JNDI InitialContext object if none exists
         * yet.
         */
        try {
                        Properties properties = new Properties();
                        properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
                        properties.put(Context.URL_PKG_PREFIXES, "org.jnp.interfaces");
                        properties.put(Context.PROVIDER_URL, "localhost");



            jndiContext = new InitialContext(properties);
        } catch (NamingException e) {
            System.out.println("Could not create JNDI " +
                "context: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }
        
        /* 
         * Look up connection factory and topic.  If either does
         * not exist, exit.
         */
        try {
            topicConnectionFactory = (TopicConnectionFactory)
                jndiContext.lookup("ConnectionFactory");
            topic = (Topic) jndiContext.lookup(topicName);
        } catch (NamingException e) {
            System.out.println("JNDI lookup failed: " +
                e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create publisher and text message.
         * Send messages, varying text slightly.
         * Finally, close connection.
         */
        try {
            topicConnection = 
                topicConnectionFactory.createTopicConnection();
            topicSession = 
                topicConnection.createTopicSession(false, 
                    Session.AUTO_ACKNOWLEDGE);
            topicPublisher = topicSession.createPublisher(topic);


            
            
            message = topicSession.createObjectMessage();

            message.setObject(new Platine(1));
            
            
            System.out.println("Publishing message: " + 
                    message.getObject().toString());
                topicPublisher.publish(message);
            
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + 
                e.toString());
        } finally {
            if (topicConnection != null) {
                try {
                    topicConnection.close();
                } catch (JMSException e) {}
            }
        }
    }
}
